package com.svo.weitu.model.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper {

	private static int version = 5;//2013年10月24日
	private static DbHelper dbHelper;
	private DbHelper(Context context) {
		super(context, "weitu.db", null, version );
	}
	public static DbHelper getInstance(Context context) {
		if (dbHelper == null) {
			dbHelper = new DbHelper(context);
		}
		return dbHelper;
	}
	private String sql1 = "CREATE TABLE IF NOT EXISTS  res(_id INTEGER PRIMARY KEY AUTOINCREMENT,name Text ,picUri Text,type INTEGER,parId INTEGER,url Text);";
	private String sql2 = "CREATE TABLE IF NOT EXISTS  list(_id INTEGER PRIMARY KEY AUTOINCREMENT,title Text,parId INTEGER,description Text,thumbnail text,srcurl Text,insert_time INTEGER unique,img_urls Text,loves INTEGER,isHome Text);";
	private String sql3 = "CREATE TABLE IF NOT EXISTS  sub(_id INTEGER PRIMARY KEY AUTOINCREMENT,name Text ,picUri Text,type INTEGER,parId INTEGER,url Text);";
	private String sql4 = "CREATE TABLE IF NOT EXISTS  col(_id INTEGER PRIMARY KEY,title Text ,description Text,insert_time INTEGER,thumbnail text,urls Text);";
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(sql1);
		db.execSQL(sql2);
		db.execSQL(sql3);
		db.execSQL(sql4);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		switch (oldVersion) {
		case 1:
			db.execSQL("drop table list");
			db.execSQL("delete from res");
			db.execSQL("CREATE TABLE IF NOT EXISTS  list(_id INTEGER PRIMARY KEY AUTOINCREMENT,title Text,parId INTEGER,description Text,thumbnail text,srcurl Text,insert_time INTEGER,img_urls Text);");
		case 2:
			db.execSQL("drop table res");
			db.execSQL(sql1);
			db.execSQL("alter table list add COLUMN loves INTEGER");
			db.execSQL(sql3);
		case 3:
			db.execSQL("drop table list");
			db.execSQL("CREATE TABLE IF NOT EXISTS  list(_id INTEGER PRIMARY KEY AUTOINCREMENT,title Text,parId INTEGER,description Text,thumbnail text,srcurl Text,insert_time INTEGER unique,img_urls Text,loves INTEGER);");
			db.execSQL("CREATE TABLE IF NOT EXISTS  col(_id INTEGER PRIMARY KEY,title Text ,description Text,insert_time INTEGER,thumbnail text,urls Text)");
		case 4:
			db.execSQL("drop table list");
			db.execSQL("CREATE TABLE IF NOT EXISTS  list(_id INTEGER PRIMARY KEY AUTOINCREMENT,title Text,parId INTEGER,description Text,thumbnail text,srcurl Text,insert_time INTEGER unique,img_urls Text,loves INTEGER,isHome Text);");
		default:
			break;
		}
	}

}
